package com.test.exoplayertest.player.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.util.DisplayMetrics;

import com.google.android.exoplayer2.ExoPlayerLibraryInfo;

import java.text.NumberFormat;

/**
 * @author wuxiaotao on 2017/6/15.
 */

public class PlayerUtil {

    public static float width = 0;
    public static float height = 0;

    public static String getUserAgent(Context context, String applicationName) {
        String versionName;
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            versionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            versionName = "?";
        }
        return applicationName + "/" + versionName + " (Linux;Android " + Build.VERSION.RELEASE
                + ") " + ExoPlayerLibraryInfo.VERSION_SLASHY;
    }

    public static String getDuration(long timeMs) {
        long totalSeconds = (timeMs + 500) / 1000;
        if (totalSeconds < 0L) {
            totalSeconds = 0L;
        }
        long hours = 0L;
        long minutes = 0L;
        if (totalSeconds >= 60L) {
            minutes = totalSeconds / 60L;
            totalSeconds %= 60L;
        }
        if (minutes >= 60L) {
            hours = minutes / 60L;
            minutes %= 60L;
        }
        return hours == 0L ? String.format("%1$02d:%2$02d", minutes, totalSeconds) :
                String.format("%1$02d:%2$02d:%3$02d", hours, minutes, totalSeconds);
    }

    public static boolean isNetWorkAvaliable(Context context) {
        if (null == context) {
            return false;
        }
        //得到网络连接信息
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //去进行判断网络是否连接
        return manager != null && manager.getActiveNetworkInfo() != null && manager.getActiveNetworkInfo().isAvailable();
    }

    public static float getDeviceWidth(Context context) {
        if (width == 0) {
            mearsureSize(context);
        }
        return width;
    }

    public static float getDeviceHeight(Context context) {
        if (height == 0) {
            mearsureSize(context);
        }
        return height;
    }

    private static void mearsureSize(Context context) {
        DisplayMetrics display = new DisplayMetrics();
        display = context.getResources().getDisplayMetrics();
        int screenWidth = display.widthPixels;
        int screenHeight = display.heightPixels;
        width = Math.min(screenWidth, screenHeight);
        height = Math.max(screenWidth, screenHeight);
    }

    public static String getPercent(int numerator, int denominator) {
        float percent = 1.0f * numerator / denominator;
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(0);
        return nt.format(percent).toString();
    }
}
